package com.example.equipment.service.impl;

import com.example.equipment.bean.Product;
import com.example.equipment.bean.ProductExample;
import com.example.equipment.bean.enums.ResultEnum;
import com.example.equipment.bean.config.OpenApiResult;
import com.example.equipment.mapper.ProductMacMapper;
import com.example.equipment.mapper.ProductMapper;
import com.example.equipment.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductMacMapper productMacMapper;

    public Product getProductByNo(String productNo) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andProductNoEqualTo(productNo.trim());
        criteria.andEnableFlagEqualTo(true);
        List<Product> products = productMapper.selectByExample(example);
        if (products != null && !products.isEmpty()) {
            return products.get(0);
        }
        return null;
    }

    @Override
    public OpenApiResult addProduct(Product product) {
        //判断是否已经存在
        Product earlyProduct = getProductByNo(product.getProductNo());
        if (earlyProduct != null) {
            return new OpenApiResult(ResultEnum.DATA_ALREADY_EXISTS);
        }
        //新增
        int count = productMapper.insertSelective(product);
        if (count > 0) {
            return OpenApiResult.ok();
        }
        return OpenApiResult.fail();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Product product) {
        Product tmp = productMapper.selectByPrimaryKey(product.getId());
        if (tmp == null) {
            return;
        }
        tmp.setEnableFlag(false);
        tmp.setUpdateBy(product.getUpdateBy());
        tmp.setUpdateTime(new Date());
        tmp.setDelMark(tmp.getId());
        productMapper.updateByPrimaryKeySelective(tmp);
        //todo 删除产品下所有数据
        productMacMapper.deleteByProductId(tmp.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Product product) {
        Product tmp = productMapper.selectByPrimaryKey(product.getId());

        if (!StringUtils.isEmpty(product.getProductNo())
                && !tmp.getProductNo().equals(product.getProductNo())) {
            Product early = productMapper.selectByProductNo(product.getProductNo());
            if (early != null) {
                throw new RuntimeException("需要修改产品编号为:" + product.getProductNo() + ".已经存在，不允许修改");
            }
            productMacMapper.updateProductNoByProductId(product.getId(), product.getProductNo());
        }
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public List<Product> getList(String productNo, String productName) {
        Product product = new Product();
        product.setProductNo(productNo);
        product.setProductName(productName);
        return productMapper.selectBySelective(product);
    }
}
